package com.ujoku.controller;

import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.util.StringUtils;
import com.ujoku.request.StoreSearchForm;
import com.ujoku.service.CdnService;
import com.ujoku.view.domain.StoreView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

import com.ujoku.domain.Store;
import com.ujoku.service.StoreService;

import javax.validation.Valid;


@RestController
public class StoreController extends RESTController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private CdnService cdnService;

    @RequestMapping(value="/store", method = RequestMethod.POST)
    public StoreView storeList(@RequestBody @Valid StoreSearchForm form){

        List<Store> storeList = new ArrayList<>();

        storeList = storeService.getStores(form.getAddress());



        for (Store store : storeList){

            String image = "store/" + store.getId() + ".jpg";

            store.setStoreImage(cdnService.builder(image, store.getUpdateTime().getTime()));

        }

        StoreView storeView = new StoreView();

        storeView.setList(storeList);
        storeView.setCount(storeService.selectCount(null));

        return storeView;
    }


    @RequestMapping(value = "/store/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Store getById(@PathVariable String id){

        return (Store)storeService.selectById(id);
    }

}
